//
//  BDMyOrderCell.m
//  iOS_beboy
//
//  Created by wen on 2017/9/20.
//  Copyright © 2017年 com.8xiong. All rights reserved.
//

#import "BDMyOrderCell.h"

@implementation BDMyOrderTitleView

- (instancetype)initWithFrame:(CGRect)frame {
    if (frame.size.width == 0 && frame.size.height == 0) {
        frame.size.width = kScreenWidth;
        frame.size.height = 1;
    }
    self = [super initWithFrame:frame];
    self.backgroundColor = [UIColor clearColor];
    
    _titleLabel = [YYLabel new];
    _titleLabel.clipsToBounds = YES;
    _titleLabel.textVerticalAlignment = YYTextVerticalAlignmentTop;
    _titleLabel.displaysAsynchronously = YES;
    _titleLabel.ignoreCommonProperties = YES;
    _titleLabel.fadeOnAsynchronouslyDisplay = NO;
    _titleLabel.fadeOnHighlight = NO;
    [self addSubview:_titleLabel];
    
    _line = [[UIView alloc] init];
    _line.backgroundColor = [UIColor colorWithHexString:@"#e5e5e5"];
    [self addSubview:_line];
    
    return self;
}

- (void)setLayout:(BDMyOrderLayout *)layout{
    self.height = layout.titleHeight;
    
    _titleLabel.size = layout.titleLayout.textBoundingSize;
    _titleLabel.textLayout = layout.titleLayout;
    _titleLabel.left = 10;
    _titleLabel.centerY = self.height / 2.0;
    
    _line.width = self.width;
    _line.height = 1;
    _line.bottom = self.height;
}

@end

@implementation BDMyOrderToolbar

- (instancetype)initWithFrame:(CGRect)frame {
    if (frame.size.width == 0 && frame.size.height == 0) {
        frame.size.width = kScreenWidth;
        frame.size.height = 1;
    }
    self = [super initWithFrame:frame];
    self.backgroundColor = [UIColor clearColor];
    
    _line = [[UIView alloc] init];
    _line.backgroundColor = [UIColor colorWithHexString:@"#e5e5e5"];
    [self addSubview:_line];
    
    _toolLabel = [YYLabel new];
    _toolLabel.clipsToBounds = YES;
    _toolLabel.textVerticalAlignment = YYTextVerticalAlignmentTop;
    _toolLabel.displaysAsynchronously = YES;
    _toolLabel.ignoreCommonProperties = YES;
    _toolLabel.fadeOnAsynchronouslyDisplay = NO;
    _toolLabel.fadeOnHighlight = NO;
    [self addSubview:_toolLabel];
    
    return self;
}

- (void)setLayout:(BDMyOrderLayout *)layout{
    self.height = layout.toolHeight;
    
    _line.width = self.width - 20;
    [_line drawLineLength:5 lineSpacing:2 lineColor:[UIColor colorWithHexString:@"#e5e5e5"] verticality:NO];
    _line.height = 1;
    _line.left = 10;
    _line.top = 0;
    
    _toolLabel.size = layout.toolLayout.textBoundingSize;
    _toolLabel.textLayout = layout.toolLayout;
    _toolLabel.left = 10;
    _toolLabel.centerY = self.height / 2.0;
}

@end

@implementation BDMyOrderView

- (instancetype)initWithFrame:(CGRect)frame {
    if (frame.size.width == 0 && frame.size.height == 0) {
        frame.size.width = kScreenWidth;
        frame.size.height = 1;
    }
    self = [super initWithFrame:frame];
    self.backgroundColor = [UIColor clearColor];
    self.exclusiveTouch = YES;
    
    _contentView = [UIView new];
    _contentView.width = kScreenWidth;
    _contentView.height = 1;
    _contentView.backgroundColor = [UIColor whiteColor];
    _contentView.clipsToBounds = YES;
    static UIImage *topLineBG, *bottomLineBG;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        topLineBG = [UIImage imageWithSize:CGSizeMake(1, 3) drawBlock:^(CGContextRef context) {
            CGContextSetFillColorWithColor(context, [UIColor blackColor].CGColor);
            CGContextSetShadowWithColor(context, CGSizeMake(0, 0), 0.8, [UIColor colorWithWhite:0 alpha:0.08].CGColor);
            CGContextAddRect(context, CGRectMake(-2, 3, 4, 4));//colorWithWhite:0 alpha:0.08
            CGContextFillPath(context);
        }];
        bottomLineBG = [UIImage imageWithSize:CGSizeMake(1, 3) drawBlock:^(CGContextRef context) {
            CGContextSetFillColorWithColor(context, [UIColor blackColor].CGColor);
            CGContextSetShadowWithColor(context, CGSizeMake(0, 0.4), 2, [UIColor colorWithWhite:0 alpha:0.08].CGColor);
            CGContextAddRect(context, CGRectMake(-2, -2, 4, 2));
            CGContextFillPath(context);
        }];
    });
    UIImageView *topLine = [[UIImageView alloc] initWithImage:topLineBG];
    topLine.width = _contentView.width;
    topLine.bottom = 0;
    topLine.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin;
    [_contentView addSubview:topLine];
    
    
    UIImageView *bottomLine = [[UIImageView alloc] initWithImage:bottomLineBG];
    bottomLine.width = _contentView.width;
    bottomLine.top = _contentView.height;
    bottomLine.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
    [_contentView addSubview:bottomLine];
    
    [self addSubview:_contentView];
    
    _titleView = [BDMyOrderTitleView new];
    [_contentView insertSubview:_titleView atIndex:0];
    
    __weak BDMyOrderView *weak_self = self;
    _userButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _userButton.titleLabel.font = [UIFont systemFontOfSize:14];
    _userButton.size = CGSizeMake(kBDOrderCellContentButtonWidth, 30);
    _userButton.layer.masksToBounds = YES;
    _userButton.layer.cornerRadius = 5;
    [_userButton setEnlargeEdgeWithTop:10 right:10 bottom:10 left:10];
    [_userButton addBlockForControlEvents:UIControlEventTouchUpInside block:^(id sender) {
        if ([weak_self.cell.delegate respondsToSelector:@selector(cellDidClickButton:)]) {
            [weak_self.cell.delegate cellDidClickButton:weak_self.cell];
        }
    }];
    [_userButton setEnlargeEdgeWithTop:10 right:10 bottom:10 left:10];
    [_contentView addSubview:_userButton];
    
    _contentImageView = [[UIImageView alloc] init];
    [_contentView addSubview:_contentImageView];
    _contentImageView.size = CGSizeMake(kBDOrderCellContentImageWidth, kBDOrderCellContentImageHeight);
    _contentImageView.layer.masksToBounds = YES;
    _contentImageView.layer.cornerRadius = 5;
    
    _contentLabel = [YYLabel new];
    _contentLabel.clipsToBounds = YES;
    _contentLabel.textVerticalAlignment = YYTextVerticalAlignmentTop;
    _contentLabel.displaysAsynchronously = YES;
    _contentLabel.ignoreCommonProperties = YES;
    _contentLabel.fadeOnAsynchronouslyDisplay = NO;
    _contentLabel.fadeOnHighlight = NO;
    [_contentView addSubview:_contentLabel];
    
    _priceLabel = [YYLabel new];
    _priceLabel.clipsToBounds = YES;
    _priceLabel.left = _contentImageView.right + kBDOrderCellContentTextPadding;
    _priceLabel.width = kBDOrderCellContentTextWidth;
    _priceLabel.textVerticalAlignment = YYTextVerticalAlignmentTop;
    _priceLabel.displaysAsynchronously = YES;
    _priceLabel.ignoreCommonProperties = YES;
    _priceLabel.fadeOnAsynchronouslyDisplay = NO;
    _priceLabel.fadeOnHighlight = NO;
    [_contentView addSubview:_priceLabel];
    
    _toolbarView = [BDMyOrderToolbar new];
    [_contentView addSubview:_toolbarView];
    
    return self;
}

- (void)setLayout:(BDMyOrderLayout *)layout {
    _layout = layout;
    self.height = layout.height;
    
    _contentView.top = layout.marginTop;
    _contentView.height = self.height - layout.marginTop - layout.marginBottom;
    
    [_titleView setLayout:layout];
    
    CGFloat top = _titleView.bottom;
    _contentImageView.left = 10;
    _contentImageView.top = kBDOrderCellContentImagePadding + top;
    [_contentImageView sd_setImageWithURL:[NSURL URLWithString:layout.contentImage] placeholderImage:[UIImage imageNamed:layout.contentImagePlaceholder]];
    
    _contentLabel.size = layout.contentLayout.textBoundingSize;
    _contentLabel.textLayout = layout.contentLayout;
    _contentLabel.left = _contentImageView.right + kBDOrderCellContentTextPadding;
    _contentLabel.top = _contentImageView.top + 5;
    
    _priceLabel.size = layout.priceLayout.textBoundingSize;
    _priceLabel.textLayout = layout.priceLayout;
    _priceLabel.left = _contentImageView.right + kBDOrderCellContentTextPadding;
    _priceLabel.bottom = _contentImageView.bottom - 5;
    
    [_userButton setTitle:layout.buttonTitle forState:UIControlStateNormal];
    [_userButton setTitleColor:layout.buttonTitleColor forState:UIControlStateNormal];
    _userButton.backgroundColor = layout.buttonBKColor;
    _userButton.userInteractionEnabled = layout.buttonCanClick;
    _userButton.right = self.width - 10;
    _userButton.centerY = _contentImageView.centerY;
    
    top = _contentImageView.bottom + kBDOrderCellContentImagePadding;
    
    _toolbarView.hidden = YES;
    if (layout.toolHeight) {
        _toolbarView.hidden = NO;
        [_toolbarView setLayout:layout];
        _toolbarView.top = top;
    }
    
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [(_contentView) performSelector:@selector(setBackgroundColor:) withObject:kBDOrderCellHighlightColor afterDelay:0.15];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    [self touchesRestoreBackgroundColor];
    if ([_cell.delegate respondsToSelector:@selector(cellDidClick:)]) {
        [_cell.delegate cellDidClick:_cell];
    }
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
    [self touchesRestoreBackgroundColor];
}

- (void)touchesRestoreBackgroundColor {
    [NSObject cancelPreviousPerformRequestsWithTarget:_contentView selector:@selector(setBackgroundColor:) object:kBDOrderCellHighlightColor];
    _contentView.backgroundColor = [UIColor whiteColor];
}


@end

@implementation BDMyOrderCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    _orderView = [BDMyOrderView new];
    [self.contentView addSubview:_orderView];
    _orderView.cell = self;
    self.backgroundColor = [UIColor colorWithHexString:@"#f2f2f2"];
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    return self;
}

- (void)setLayout:(BDMyOrderLayout *)layout{
    self.height = layout.height;
    self.contentView.height = layout.height;
    [_orderView setLayout:layout];
}

@end
